# -*- coding: utf-8 -*-
# @File  : canshuhua_login.py
# @Author: zbh
# @Date  : 2025/3/28_16:31



# 测试登录接口，通过读取test_canshu.yaml的配置文件进行测试
import pytest
import requests
import yaml
import logging
import allure
from urllib.parse import urlencode
from time import sleep
from python_basics.api_fengzhuang.func_logging import setup_logger



# 调用日志函数
logger = setup_logger()

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': 'JSESSIONID=0000AvnUjkttGwgEVYsgZqLsj98:-1; acp_cookie_name=84133240BA1C877FFD69A0CE841D1BEEB387CB3911B15CD0C22CE7BA'
}
with open('test_canshu.yaml', 'r', encoding='utf-8') as f:
    config = yaml.safe_load(f)
    # print(config)
    # print(config.get('test_case'))
    # # print(config)
    # # print(config.get('test_case')[0].get('data'))
    # # print(config.get('test_case')[1].get('data'))
    # # logger.info("开始测试登录接口")
    # logger.info(f"请求地址是:{config.get('url')}")
    # for i in config.get('test_case'):
    #     # print(i.get('data'))
    #     # 把json格式转换为form表单
    #     form_data = urlencode(i.get('data'))  # 把json格式转换为form表单
    #     logger.info(f"请求参数是:{form_data}")
    #     # print(form_data)
    #     # resp = requests.request('post',url=url,data= form_data,headers=headers)
    #     # resp = requests.post(url=config.get('url'), data=i.get('data'), headers=headers)
    #     # print(i.get('data'))


    @pytest.mark.parametrize("data", config.get('test_case')) # 读取yaml文件中的数据，参数data为测试数据
    @allure.feature("登录功能") # allure报告的一级标题
    @allure.story("登录接口") # allure报告的二级标题
    @allure.title("登录接口测试") # allure报告的标题
    @allure.description("登录接口测试")
    def test_login(data):
        logger.info("开始登录接口测试")
        resp = requests.post(url=config.get('url'), data=config.get('data'), headers=headers)
        logger.info(f"响应结果是:{resp.status_code}")
        assert resp.status_code == 200







